package com.hymjweb.demo.hymj.customer_manage.invoice.mapper;

import com.hymjweb.demo.hymj.customer_manage.invoice.bean.BCustomerInvoicePojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface BCustomerInvoiceMapper {

    class BCustomerInvoiceMapperProvider{

        public String queryInvoiceByCustomerId(String customerId){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_invoice t ");
                WHERE("t.customer_id = #{customerId}");
            }}.toString();
            return  sql;
        }

        public String insert(BCustomerInvoicePojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("b_customer_invoice");
                VALUES("id","#{id}");
                VALUES("customer_id","#{customerId}");
                VALUES("invoice_title","#{invoiceTitle}");
                VALUES("taxpayer_identification_number","#{taxpayerIdentificationNumber}");
                VALUES("phone_number","#{phoneNumber}");
                VALUES("invoice_address","#{invoiceAddress}");
                VALUES("open_account_bank_id","#{openAccountBankId}");
                VALUES("bank_account","#{bankAccount}");
                VALUES("invoice_type_id","#{invoiceTypeId}");
                VALUES("rate_id","#{rateId}");
                VALUES("recipient_name","#{recipientName}");
                VALUES("recipient_phone","#{recipientPhone}");
                VALUES("recipient_address","#{recipientAddress}");
            }}.toString();
            return sql;
        }

        public String update(BCustomerInvoicePojo pojo) {
            String sql= new SQL() {{
                UPDATE("b_customer_invoice t");
                SET("t.customer_id = #{customerId}");
                SET("t.invoice_title = #{invoiceTitle}");
                SET("t.taxpayer_identification_number = #{taxpayerIdentificationNumber}");
                SET("t.phone_number = #{phoneNumber}");
                SET("t.invoice_address = #{invoiceAddress}");
                SET("t.open_account_bank_id = #{openAccountBankId}");
                SET("t.bank_account = #{bankAccount}");
                SET("t.invoice_type_id = #{invoiceTypeId}");
                SET("t.rate_id = #{rateId}");
                SET("t.recipient_name = #{recipientName}");
                SET("t.recipient_phone = #{recipientPhone}");
                SET("t.recipient_address = #{recipientAddress}");
                WHERE("t.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String invoiceId) {
            String sql = new SQL() {{
                DELETE_FROM("b_customer_invoice t");
                WHERE("t.id = #{invoiceId}");
            }}.toString();
            return sql;
        }

    }

    @Results(id = "bCustomerInvoiceMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "customer_id", property = "customerId"),
            @Result(column = "invoice_title", property = "invoiceTitle"),
            @Result(column = "taxpayer_identification_number", property = "taxpayerIdentificationNumber"),
            @Result(column = "phone_number", property = "phoneNumber"),
            @Result(column = "invoice_address", property = "invoiceAddress"),
            @Result(column = "open_account_bank_id", property = "openAccountBankId"),
            @Result(column = "bank_account", property = "bankAccount"),
            @Result(column = "invoice_type_id", property = "invoiceTypeId"),
            @Result(column = "rate_id", property = "rateId"),
            @Result(column = "recipient_name", property = "recipientName"),
            @Result(column = "recipient_phone", property = "recipientPhone"),
            @Result(column = "recipient_address", property = "recipientAddress")
    })
    @SelectProvider(type = BCustomerInvoiceMapperProvider.class, method = "queryInvoiceByCustomerId")
    List<BCustomerInvoicePojo> queryInvoiceByCustomerId(String customerId);

    @InsertProvider(type = BCustomerInvoiceMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BCustomerInvoicePojo pojo);

    @UpdateProvider(type = BCustomerInvoiceMapperProvider.class, method = "update")
    int update(BCustomerInvoicePojo pojo);

    @DeleteProvider(type = BCustomerInvoiceMapperProvider.class, method = "delete")
    int delete(String invoiceId);
}
